﻿<%@ Page Language="C#" %>
<%@ Import Namespace="IRL.Utilities" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
    public const int CURRENT_MODULE = 52;
    bool HasWriteAccess = false;


    protected void Page_Load(object sender, EventArgs e)
    {
        Security SysAcc = new Security(CURRENT_MODULE);
        // check is authenticate
        SysAcc.Authenticate();
        HasWriteAccess = SysAcc.IsAuthorized(2);

        if (Page.IsPostBack)
        {
            int clientId = 0;
            if (HasWriteAccess)
            {
                clientId = saveData();

                lblSaveStatus.Text = "Data is already saved to the database.<br //><br //>";
            }
            if (ViewState["HTTP_REFERER"] != null)
            {
                string url = ViewState["HTTP_REFERER"].ToString();
                //if (!string.IsNullOrEmpty(url))
                //{
                    //url += "?cid=" + clientId.ToString();
                    Response.Redirect(url);
                //}                
            }
            else
            {
                Response.Redirect("default2.aspx");
            }

        }
        else
        {
            loadData();
            ViewState.Add("HTTP_REFERER", Request.ServerVariables["HTTP_REFERER"].ToString());
        }
    }

    int saveData()
    {
        string companyName = txtCompanyName.Text;
        string Abbreviation = txtAbbreviation.Text;
        int CompanyType = Convert.ToInt32(cboType.SelectedValue);
        int Sector = Convert.ToInt32(cboSector.SelectedValue);
        string Phone1 = txtPhone1_1.Text + " " + txtPhone1_2.Text + " " + txtPhone1_3.Text;
        string Phone2 = txtPhone2_1.Text + " " + txtPhone2_2.Text + " " + txtPhone2_3.Text;
        string Fax = txtFax_1.Text + " " + txtFax_2.Text + " " + txtFax_3.Text;
        string Website = txtWebsite.Text;
        string Email = txtEmail.Text;
        string House_no = txtHouse_no.Text;
        string StreetName = txtStreetName.Text;
        string Address = txtAddress.Text;
        string City = txtCity.Text;
        int Country = Convert.ToInt32(cboCountry.SelectedValue);
        string Note = txtNote.Text;

        object connStr = ConfigurationSettings.AppSettings["sqlConn"];
        using (SqlConnection conn = new SqlConnection(connStr.ToString()))
        {
            SqlCommand cmd = new SqlCommand("sp_SaveCompany", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Company_Name", SqlDbType.NVarChar, 100).Value = companyName;
            cmd.Parameters.Add("@Abbreviation", SqlDbType.VarChar, 10).Value = Abbreviation;
            cmd.Parameters.Add("@Company_Type", SqlDbType.Int).Value = CompanyType;
            cmd.Parameters.Add("@Sector", SqlDbType.Int).Value = Sector;
            cmd.Parameters.Add("@Phone1", SqlDbType.VarChar, 14).Value = Phone1.Trim();
            cmd.Parameters.Add("@Phone2", SqlDbType.VarChar, 14).Value = Phone2.Trim();
            cmd.Parameters.Add("@Fax", SqlDbType.VarChar, 14).Value = Fax.Trim();
            cmd.Parameters.Add("@Website", SqlDbType.VarChar, 120).Value = Website;
            cmd.Parameters.Add("@Email", SqlDbType.VarChar, 50).Value = Email;
            cmd.Parameters.Add("@house", SqlDbType.VarChar, 10).Value = House_no;
            cmd.Parameters.Add("@street_Name", SqlDbType.NVarChar, 30).Value = StreetName;
            cmd.Parameters.Add("@address", SqlDbType.NVarChar, 255).Value = Address;
            cmd.Parameters.Add("@city", SqlDbType.NVarChar, 30).Value = City;
            cmd.Parameters.Add("@country_Reg", SqlDbType.Int).Value = Country;
            cmd.Parameters.Add("@note", SqlDbType.NVarChar, 255).Value = Note;
            conn.Open();
            return (int)cmd.ExecuteScalar();
            //cmd.ExecuteNonQuery();
        }
    }

    void loadData()
    {
        List.fillList(cboType, "sp_Select_cms_Lookup 'client'");
        List.fillList(cboSector, "sp_Select_cms_Lookup 'client_type'");
        List.fillList(cboCountry, "sp_Select_cms_Lookup 'country_region'");
        if (!HasWriteAccess)
        {
            SaveButton.Visible = false;
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    <title>CMS - Client</title>
    <link type="text/css" rel="stylesheet" href="../css/global.css" />
    <link type="text/css" rel="stylesheet" href="css/cms.css" />
    
    <script language="javascript" type="text/javascript" src="includes/functions.js"></script>
    
    
    <script language="javascript">
        function Save() {
	        var f = document.DataForm;
	        missinginfo = "";
        	
	        // Check Company name
	        if(f.txtCompanyName.value == ""){
		        missinginfo += "\t- Company name \n";
	        }
	        // Check Company type
	        if(f.cboType.value == ""){
		        missinginfo += "\t- Relationship with IRL \n";
	        }
        			
	        // Check Sector
	        if(f.cboSector.value==""){
		        missinginfo += "\t- Sector\n";
	        }
        	
	        // Check Phone1
	        if((f.txtPhone1_1.value.length+f.txtPhone1_2.value.length+f.txtPhone1_3.value.length)>0){
		        if(!f.txtPhone1_1.value.match(/^\d+$/) || f.txtPhone1_1.value.length<3){
			        f.txtPhone1_1.select();
			        missinginfo += "\t- Phone Number\n";
		        }else if(!f.txtPhone1_2.value.match(/^\d+$/) || f.txtPhone1_2.value.length<2){
			        f.txtPhone1_2.select();
			        missinginfo += "\t- Phone Number\n";
		        }else if(!f.txtPhone1_3.value.match(/^\d+$/) || f.txtPhone1_3.value.length<6){
			        f.txtPhone1_3.select();
			        missinginfo += "\t- Phone Number\n";
		        }
	        }
        	
	        // Check Phone2
	        if((f.txtPhone2_1.value.length+f.txtPhone2_2.value.length+f.txtPhone2_3.value.length)>0){
		        if(!f.txtPhone2_1.value.match(/^\d+$/) || f.txtPhone2_1.value.length<3){
			        f.txtPhone2_1.select();
			        missinginfo += "\t- Other Phone\n";
		        }else if(!f.txtPhone2_2.value.match(/^\d+$/) || f.txtPhone2_2.value.length<2){
			        f.txtPhone2_2.select();
			        missinginfo += "\t- Other Phone\n";
		        }else if(!f.txtPhone2_3.value.match(/^\d+$/) || f.txtPhone2_3.value.length<6){
			        f.txtPhone2_3.select();
			        missinginfo += "\t- Other Phone\n";
		        }
	        }
        	
	        // Check Fax
	        if((f.txtFax_1.value.length+f.txtFax_2.value.length+f.txtFax_3.value.length)>0){
		        if(!f.txtFax_1.value.match(/^\d+$/) || f.txtFax_1.value.length<3){
			        f.txtFax_1.select();
			        missinginfo += "\t- Fax Number\n";
		        }else if(!f.txtFax_2.value.match(/^\d+$/) || f.txtFax_2.value.length<2){
			        f.txtFax_2.select();
			        missinginfo += "\t- Fax Number\n";
		        }else if(!f.txtFax_3.value.match(/^\d+$/) || f.txtFax_3.value.length<6){
			        f.txtFax_3.select();
			        missinginfo += "\t- Fax Number\n";
		        }
	        }
	        
	        // Check Email
	        if(f.txtEmail.value.length>0)
	        {
	            if(!validEmail(f.txtEmail.value))
	            {
	                f.txtEmail.select();
	                missinginfo += "\t- Email\n";
	            }
	        }
        	
        	
	        if (missinginfo != "") {
		        msg = "__________________________\n";
		        msg += "You failed to correctly fill in your:\n\n";
		        msg += missinginfo;
		        msg += "_________________________\n";
		        msg += "Please complete mandatory fields before saving";
		        alert(msg);
		        return ;
	        }else
		        msg = "This Information has been saved to the database";
        		
        	
	        //alert(msg);
	        f.submit();
	        //return true;
        }
    </script>

</head>

<body>
<table width="773" border="0" cellspacing="0" cellpadding="0" align="center">
	<tr>
		<td width="765" height="540" align="center" valign="top" style="border:solid 1px #10147e;" >
			<!-- header -->
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
				<tr bgcolor="#F4F4F9">
					<td colspan="3" align="right" style="padding:10px;">
						<b class="blueL">Client Management System</b>
					</td>
				</tr>
				<tr>
					<td class="wave_l">&nbsp;</td>
					<td><img src="images/wave_logo.jpg" width="107" height="83"></td>
					<td class="wave_r">&nbsp;</td>
				</tr>
				<tr>
					<td colspan="3" style="padding:0 20px;">
						<div style="background-color:#bcb3d5; padding:5px 10px;">
							<span style="float:right">
							    <a class="blue" href="javascript:history.go(-1);">&laquo; Back</a>
							</span>
							<span class="pathmap">
								<a href="default.aspx" >CPanel</a> :: 
								<a href="default2.aspx" >Ops Files</a> :: 								
								Save new Client
						  </span>
						</div>
						<br />
						
						
					</td>
				</tr>
				
			</table>
			
			<!-- body -->
			<form id="DataForm" method="post" runat="server">
			<table border="0" width="100%" cellspacing="0" cellpadding="0">
				<tr>
					<td align="center" valign="top" style="padding-left:20px;">
                        <asp:Label CssClass="orange" ID="lblSaveStatus" runat="server" />
                        <table border="0" cellspacing="0" cellpadding="3">
							<tr>
								<td align="right">Company Name </td>
								<td>:</td>
								<td><asp:TextBox id="txtCompanyName" size="32" runat="server" /> 
								<span class="red">*</span> &nbsp;
								Abbreviation : 
								<asp:TextBox id="txtAbbreviation" size="6" maxlength="10" runat="server" /></td>
							</tr>
							<tr>
						        <td align="right">Relationship with IRL </td>
						        <td>:</td>
						        <td><asp:DropDownList id="cboType" runat="server" />
							      <span class="red">*</span></td>
						    </tr>
                            <tr>
                                <td align="right">Sector  </td>
                                <td>:</td>
                                <td><asp:DropDownList id="cboSector" runat="server" />
                                <span class="red">*</span></td>
                            </tr>
                            <tr>
                                <td align="right">Phone Number  </td>
                                <td>:</td>
                                <td>+ <asp:TextBox ID="txtPhone1_1" CssClass="date" size="2" MaxLength="3" runat="server" />
                                      <asp:TextBox ID="txtPhone1_2" CssClass="date" size="1" MaxLength="2" runat="server" />
                                      <asp:TextBox ID="txtPhone1_3" CssClass="date" size="8" MaxLength="8" runat="server" />
                                      <span class="fldNote">Example: +855 23 215184</span></td>
                            </tr>
                            <tr>
                                <td align="right">Other Phone  </td>
                                <td>:</td>
                                <td>+ <asp:TextBox ID="txtPhone2_1" CssClass="date" size="2" MaxLength="3" runat="server" />
                                      <asp:TextBox ID="txtPhone2_2" CssClass="date" size="1" MaxLength="2" runat="server" />
                                      <asp:TextBox ID="txtPhone2_3" CssClass="date" size="8" MaxLength="8" runat="server" />
                                      <span class="fldNote">Example: +855 23 215184</span></td>
                            </tr>
                            <tr>
                                <td align="right">Fax Number  </td>
                                <td>:</td>
                                <td>+ <asp:TextBox ID="txtFax_1" CssClass="date" size="2" MaxLength="3" runat="server" />
                                      <asp:TextBox ID="txtFax_2" CssClass="date" size="1" MaxLength="2" runat="server" />
                                      <asp:TextBox ID="txtFax_3" CssClass="date" size="8" MaxLength="8" runat="server" />
                                      <span class="fldNote">Example: +855 23 215184</span></td>
                            </tr>
                            <tr>
                                <td align="right">Website</td>
                                <td>:</td>
                                <td><asp:TextBox ID="txtWebsite" size="28" runat="server" />
                                <span class="fldNote">Eg. www.indochinaresearch.com</span></td>
                            </tr>
                            <tr>
                                <td align="right">Email</td>
                                <td>:</td>
                                <td><asp:TextBox ID="txtEmail" size="30" MaxLength="50" runat="server" /></td>
                            </tr>
                            <tr>
                                <td align="right">Street # </td>
                                <td>:</td>
                                <td><asp:TextBox ID="txtHouse_no" size="5" MaxLength="10" runat="server" />

                                St. Name :
                                <asp:TextBox ID="txtStreetName" size="18" MaxLength="30" runat="server" /></td>
                            </tr>
                            <tr>
                                <td align="right">Address</td>
                                <td>:</td>
                                <td><asp:TextBox ID="txtAddress" size="60" MaxLength="255" runat="server" /></td>
                            </tr>
                            <tr>
                                <td align="right">City</td>
                                <td>:</td>
                                <td><asp:TextBox ID="txtCity" size="15" MaxLength="30" runat="server" /> &nbsp;
                                Country :
                                <asp:DropDownList ID="cboCountry" runat="server" />
                                  <span class="red">*</span>
                                </td>
                            </tr>

                            <tr>
                                <td align="right">Note</td>
                                <td>:</td>
                                <td><asp:TextBox ID="txtNote" Rows="3" Columns="60" TextMode="multiline" runat="server" /></td>
                            </tr>
                            
                            <tr>
							    <td align="center" colspan="3">&nbsp;</td>
							</tr>
							
							<tr>
							  	<td colspan="2">&nbsp;</td>
								<td>
									<table id="SaveButton" border="0" cellpadding="2" cellspacing="0" runat="server" >
										<tr>
											<td width="70">
												<a class="cmd" href="javascript:Save();">Save</a></td>
											<td width="5">&nbsp;</td>
											<td width="80">
												<a class="cmd" href="javascript:history.go(-1);">Cancel</a></td>
										</tr>
									</table>
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table><br />
			</form>
			
		</td>
		
		<td width="6" class="rightShadow" valign="top">
			<img src="images/white.gif" width="6" height="6" /></td>
	</tr>
	
	<!-- bottom shadow -->
	<tr>
		<td height="6" class="bottomShadow">
			<img src="images/white.gif" width="6" height="6" /></td>
		
		<td width="6" height="6">
			<img src="images/shadow_corner.gif" width="6" height="6" /></td>
	</tr>
</table>


</body>
</html>
